package cc.wsyw126.java.lintCode.backpack;

/**
 * Created by junpeng.yang on 17/3/22.
 */
public class Solution {
    int getMax(int a, int b) {
        return a > b ? a : b;
    }

    public int backPackII(int m, int[] A, int V[]) {
        int n = A.length;
        int[][] max = new int[n][m + 1];
        for (int i = 1; i < n; ++i) {
            for (int j = 0; j <= m; ++j) {
                max[i][j] = 0;
            }
        }
        for (int l = 0; l <= m; ++l) {
            if (l < A[0]) {
                max[0][l] = 0;
            } else {
                max[0][l] = V[0];
            }
        }
        for (int i = 1; i < n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (j < A[i]) {
                    max[i][j] = max[i - 1][j];
                } else {
                    max[i][j] = getMax(max[i - 1][j - A[i]] + V[i], max[i - 1][j]);
                }
            }
        }

        for (int k = 0; k < n; ++k) {
            for (int i = 1; i <= m; ++i) {
                System.out.print(max[k][i] + "\t");
            }
            System.out.println();
        }
        System.out.println(max[n - 1][m]);
        return max[n - 1][m];
    }
}
